package me.xplabs.magic 
{
	import me.xplabs.gemo.Point2D;
	/**
	 * http://www.xplabs.me/
	 * @author xiaohan
	 * 直线的技能特效
	 */
	public class StraightMagic extends BaseMagicPath
	{
		private var _distanceX:Number;
		private var _distanceY:Number;
		public function StraightMagic() 
		{
		}
		public function initialize(targetPoint2d:Point2D, sourcePoint2d:Point2D, speed:int):void
		{
			_targetPoint2d = targetPoint2d;
			_sourcePoint2d = sourcePoint2d;
			_speed = speed;
			//_speed = 8;
		}
		
		override public function startPath():void 
		{
			_distance = _targetPoint2d.distance(_sourcePoint2d);
			_curTime = 0;
			_speed *= 6;
			//trace(_speed);
			totalTime = _distance / _speed;
			curPoint2d = _sourcePoint2d.clone();
			
			angle = Math.atan2(_targetPoint2d.y - _sourcePoint2d.y, _targetPoint2d.x - _sourcePoint2d.x);
			var tempAngle:Number = angle;
			//angle = 180 * tempAngle / Math.PI;
			_distanceX = _distance * Math.cos(angle);
			_distanceY = _distance * Math.sin(angle);
		}
		override public function updateTrack(gap:int):Boolean
		{
			_distance = _targetPoint2d.distance(_sourcePoint2d);
			_curTime += (gap/1000);
			if (_curTime >= totalTime)
			{
				curPoint2d.setPos(_targetPoint2d);
				return true;
			}
			
			curPoint2d.x = _sourcePoint2d.x + _distanceX * _curTime / totalTime;
			curPoint2d.y  = _sourcePoint2d.y + _distanceY * _curTime / totalTime;
			return false;
			
			//var dx:Number = _targetPoint2d.x - curPoint2d.x;
			//var dy:Number = _targetPoint2d.y - curPoint2d.y;
			//var tempAngle:Number = Math.atan2(dy, dx);
			//var angle:Number = tempAngle * 180 / Math.PI;
			//var vx:Number = Math.cos(tempAngle) * _speed;
			//var vy:Number = Math.sin(tempAngle) * _speed;
			//setCurPos(curPoint2d.x + vx, curPoint2d.y + vy);
			//if (Math.sqrt(dx * dx + dy * dy) > _speed) return false;
			//setCurPos(_targetPoint2d.x, _targetPoint2d.y);
			//return true;
		}
		public function setCurPos(p_x:Number, p_y:Number):void
		{
			curPoint2d.x = p_x;
			curPoint2d.y = p_y;
		}
	}

}